home *** CD-ROM | disk | FTP | other *** search
- /* OS- and machine-dependent stuff for Mac */
-
- /*mac_io.c
- * Routines for Macintosh IO and file stuff
- */
- #include <stdio.h>
- #include "global.h"
- #include "mbuf.h"
- #include "internet.h"
- #include "iface.h"
- #include "mac.h"
- #include "cmdparse.h"
- #include <ctype.h>
-
- #include "DeviceMgr.h"
- #include "WindowMgr.h"
- #include "EventMgr.h"
- #include "SerialDvr.h"
- #include "HFS.h"
- #include <time.h>
- extern long timezone;
- extern errno;
-
- struct Store_input Store_input[ASY_MAX];
-
- struct asy asy[ASY_MAX];
-
- static ioParam MacSer;
-
- struct interface *ifaces;
- struct RemoveIt Head;
-
- unsigned nasy;
-
- /* Called at startup time to set up console I/O, memory heap */
- ioinit()
- {
- timezone = 60*60+1;
- tzname[0] = "CST";
- tzname[1] = "CST";
- Click_On(0);
- mac_files();
- return(0);
- }
- /* Called just before exiting to restore console state */
- iostop()
- {
- int i;
- struct RemoveIt *rptr;
-
- while(ifaces != NULLIF){
- if(ifaces->stop != NULLFP)
- (*ifaces->stop)(ifaces);
- ifaces = ifaces->next;
- }
- /*
- * I want to close down all the files and then remove the (possibly still existing) files
- * because the MAC will not allow the file to be removed when it still has an open file
- * descriptor
- */
-
- for( i = 3; i < _NFILE; i++)
- {
- close(i);
- }
- (void)unlink("dir.temp");
- rptr = &Head;
- while( rptr->next != NULL)
- {
- if ( rptr->name_ptr != NULL)
- {
- unlink(rptr->name_ptr);
- free(rptr->name_ptr);
- }
- rptr = rptr->next;
- }
- }
-
- /* Initialize asynch port "dev" */
-
- int slipisopen = 0;
- int iref;
- int oref;
-
- char Recv_buf[MAX_STORE];
- char Send_buf[MAX_STORE];
-
- int
- asy_init(dev,bufsize)
- int16 dev;
- unsigned bufsize;
- {
- register struct asy *ap;
- register struct interface *if_asy;
- extern struct interface *ifaces;
- char ser_name[255];
- struct Store_input *store;
-
- OsErr e;
- ap = &asy[dev];
- #ifdef DEBUG
- printf("asy_init: as->tty = %s, dev = %d\n", ap->tty, dev);
- #endif
- if ( ap == NULL)
- {
- return(-1);
- }
- /*
- * the user can use either port 'a' or 'b'. 'A' is the perfered
- */
-
- switch ( ap->tty[0] )
- {
- case 'a':
- case 'A':
- if ( ap->devopen == 0 )
- {
- e = RAMSDOpen(sPortA);
- if ( e != noErr )
- {
- printf("RAMSDOpen failed, e = %d\n",e);
- SysBeep(4);
- ExitToShell();
- }
- e = OpenDriver ("\p.AIn", &iref);
- if ( e != noErr) {
- printf("OpenDriver for AIn failed, e = %d\n",e);
- SysBeep (4);
- ExitToShell ();
- }
- e = OpenDriver ("\p.AOut", &oref);
- if ( e != noErr) {
- printf("OpenDriver for AOut failed, e = %d\n",e);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
- ap->portIn = AinRefNum;
- ap->portOut = AoutRefNum;
- ap->devopen = 1;
- }
- else
- {
- printf("Device %c is already open.\n", ap->tty[0]);
- return(-1);
- }
- break;
-
- case 'b':
- case 'B':
- if ( ap->devopen == 0)
- {
- e = RAMSDOpen(sPortB);
- if ( e != noErr )
- {
- printf("RAMSDOpen failed, e = %d\n", e);
- SysBeep(4);
- ExitToShell();
- }
- e = OpenDriver ("\p.BIn", &iref);
- if ( e != noErr) {
- printf("OpenDriver for BIn failed, e = %d\n", e);
- SysBeep (4);
- ExitToShell ();
- }
- e = OpenDriver ("\p.BOut", &oref);
- if ( e != noErr) {
- printf("Opendriver for Bout failed e = %d\n",e);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
- ap->portIn = BinRefNum;
- ap->portOut = BoutRefNum;
- ap->devopen = 1;
- }
- else
- {
- printf("Device %c is already open.\n", ap->tty[0] );
- return(-1);
- }
- break;
-
- default:
- printf("Unknown device %c, could not configure port,\n", ap->tty);
- return(-1);
- }
-
- /* setup the ring buffer for input on this device */
-
- store = &Store_input[dev];
- store->head = store->store;
- store->tail = store->store;
- store->amt = 0;
- /*
- * tell the device that it can receive only MAX_STORE
- */
- e = SerSetBuf( ap->portIn, Recv_buf, MAX_STORE);
- if ( e != noErr) {
- printf("asy_init: SerSetBuf error on %d\n", ap->portIn);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
- SerSetBuf( ap->portOut, Send_buf, MAX_STORE);
- if ( e != noErr) {
- printf("asy_init: SerSetBuf error on %d\n", ap->portOut);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
- slipisopen = 1;
- return (0);
- }
-
- int
- asy_stop(interface)
- struct interface *interface;
- {
- register struct asy *ap;
- int ref;
-
- ap = &asy[interface->dev];
-
- if (slipisopen)
- {
- /*
- * kill off any io pending
- */
- MacSer.ioActCount = 0;
- MacSer.ioRefNum = ap->portIn;
- MacSer.ioCompletion = 0;
- MacSer.ioBuffer = ap->recv_buf;
- MacSer.ioReqCount = 0;
- MacSer.ioPosMode = 1;
- (void) PBKillIO(&MacSer, FALSE);
-
- MacSer.ioActCount = 0;
- MacSer.ioRefNum = ap->portOut;
- MacSer.ioCompletion = 0;
- MacSer.ioBuffer = ap->recv_buf;
- MacSer.ioReqCount = 0;
- MacSer.ioPosMode = 1;
- (void) PBKillIO(&MacSer, FALSE);
-
- FSClose(ap->portIn);
- FSClose(ap->portOut);
- if ( ap->portIn = AinRefNum)
- {
- OpenDriver("\p.Ain", &ref);
- OpenDriver("\p.Aout", &ref);
- }
- else
- {
- OpenDriver("\p.Bin", &ref);
- OpenDriver("\p.Bout", &ref);
- }
-
- slipisopen = 0;
- }
- }
-
- /* Asynchronous line I/O control */
- asy_ioctl(interface,argc,argv)
- struct interface *interface;
- int argc;
- char *argv[];
- {
- if(argc < 1){
- printf("%d\r\n",asy[interface->dev].speed);
- return 0;
- }
- return asy_speed(interface->dev,atoi(argv[0]));
- }
-
-
- /* Set asynch line speed */
- int
- asy_speed(dev,speed)
- int dev;
- int speed;
- {
- OsErr e;
- int serialconfig;
- SerShk HandShake;
- struct asy *ap;
-
-
- ap = &asy[dev];
-
- if(speed == 0 || dev >= nasy)
- return(-1);
-
- #ifdef DEBUG
- printf("asy_speed: Setting speed for device %d to %d\n",dev, speed);
- #endif
- asy[dev].speed = speed;
-
- switch(speed)
- {
- case 300:
- serialconfig = baud300;
- break;
- case 600:
- serialconfig = baud600;
- break;
- case 1200:
- serialconfig = baud1200;
- break;
- case 1800:
- serialconfig = baud1800;
- break;
- case 2400:
- serialconfig = baud2400;
- break;
- case 3600:
- serialconfig = baud3600;
- break;
- case 4800:
- serialconfig = baud4800;
- break;
- case 7200:
- serialconfig = baud7200;
- break;
- case 9600:
- serialconfig = baud9600;
- break;
- case 19200:
- serialconfig = baud19200;
- break;
- case (long)57600:
- serialconfig = baud57600;
- break;
- default:
- printf("asy_speed: Unknown speed (%ld)\n", speed);
- break;
- }
- /*
- printf("serialconfig = %d\n", serialconfig);
- */
- serialconfig |= (stop10|noParity|data8);
-
- /* not set the speed up */
-
- e = SerReset( ap->portIn, serialconfig);
- if ( e != noErr) {
- printf("asy_speed: could not set config for %d\n", ap->portIn);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
-
- bzero(&HandShake, sizeof (SerShk));
- e = SerHShake(ap->portIn, &HandShake);
- if ( e != noErr) {
- printf("asy_speed: could not set handshake for %d\n", ap->portIn);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
-
- e = SerReset( ap->portOut, serialconfig);
- if ( e != noErr) {
- printf("asy_speed: could not set config for %d\n", ap->portOut);
- SysBeep (4);
- ExitToShell ();
- }
- bzero(&HandShake, sizeof (SerShk));
- e = SerHShake(ap->portOut, &HandShake);
- if ( e != noErr) {
- printf("asy_speed: could not set handshake for %d\n", ap->portOut);
- SysBeep (4);
- CloseDriver (iref);
- ExitToShell ();
- }
- /*
- printf("asy_speed: completed.\n");
- */
- return(0);
-
- }
- /* Send a buffer to serial transmitter */
- asy_output(dev,buf,cnt)
- unsigned dev;
- char *buf;
- unsigned short cnt;
- {
- register struct asy *ap;
- long amount = (long)cnt;
- ap = &asy[dev];
- /*
- printf("asy_output called. dev = %x, cnt = %d\n", dev, cnt);
- */
- if(dev >= nasy)
- return(-1);
- FSWrite(ap->portOut,&amount, buf);
- return(0);
-
- }
-
- /*
- * Receive characters from asynch line
- * Returns count of characters read
- */
-
- unsigned
- asy_recv(dev,buf,cnt)
- int dev;
- char *buf;
- unsigned cnt;
- {
- long amount[8];
- int tot = 0;
- struct Store_input *store;
- char store_it[MAX_STORE];
- int got = 0;
- int tt;
- struct asy *ap;
-
- #ifdef DEBUG
- printf("asy_recv: dev = %d\n", dev);
- #endif
-
- if ( dev > nasy )
- {
- printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
- return(-1);
- }
-
- ap = &asy[dev];
-
- store = &Store_input[dev]; /* point to current */
-
- if ( cnt > 0 && store->amt > 0)
- {
- tt = min(cnt , store->amt);
- tt = min(tt, MAX_STORE);
- got = 0;
-
- while ( tt-- > 0)
- {
- buf[got++] = *store->tail++;
- if ( store->tail >= &store->store[MAX_STORE])
- {
- store->tail = store->store; /* wrap around */
- }
- if ( got >= cnt )
- break;
- }
- store->amt -= got;
- return( got );
- }
- else if ( cnt == 0 )
- {
- return(0);
- }
-
- /*
- * How much is waiting for us? If any is waiting, this go and get it
- */
-
- Status( ap->portIn, 2, &amount[0]);
- if ( amount[0] > 0L)
- {
- MacSer.ioRefNum = ap->portIn;
- MacSer.ioCompletion = 0;
- MacSer.ioBuffer = ap->recv_buf;
- MacSer.ioReqCount = (amount[0] < MAX_STORE) ? amount[0] : MAX_STORE;
- MacSer.ioPosMode = 1;
- tot = PBRead(&MacSer, FALSE);
- if ( MacSer.ioResult != noErr)
- {
- printf("asy_read: read failed.\n");
- }
- tt = MacSer.ioActCount;
- got = 0;
- while (tt-- > 0)
- {
- *store->head++ = ap->recv_buf[got++];
- if ( store->head == &store->store[MAX_STORE] )
- {
- /* printf("xx got wrap\n"); */
- store->head = store->store;
- }
- store->amt++;
- }
- if ( cnt > 0 && store->amt > 0)
- {
- tt = min(cnt , store->amt);
- tt = min(tt, MAX_STORE);
- got = 0;
-
- /*
- * now store it in the ring buffer
- */
- while ( tt-- > 0)
- {
- buf[got++] = *store->tail++;
- if ( store->tail >= &store->store[MAX_STORE])
- {
- store->tail = store->store; /* wrap around */
- }
- if ( got >= cnt )
- break;
- }
- store->amt -= got;
- return( got );
- }
- return ( 0 );
- }
- return (got);
-
- }
-
- char *memchr(str, chr, cnt)
- char *str;
- char chr;
- int cnt;
- {
- int i;
- char *ptr;
-
- for( ptr = str, i = 0; i < cnt; i++, ptr++)
- {
- if (*ptr == chr )
- {
- return(ptr);
- }
- }
- return(NULLCHAR);
- }